﻿namespace Jhong.FunMVCBlog.Admin.Controllers
{
    using Jhong.FunMVC;
    using Jhong.FunMVCBlog.Core.Data;
    using Jhong.FunMVCBlog.Core.Infrastructure;

    public abstract class SecurityController : BaseController
    {
        protected override bool OnAuthorization()
        {
            if (false == this.CheckLogin())
            {
                RedirectToAction("Login", new LoginController());
                return false;
            }
            return true;
        }

        private bool CheckLogin()
        {
            if (null != this.GetSession("hasLogin")) return true;
            if (string.IsNullOrWhiteSpace(Request.Cookie["UserName"])) return false;
            var token = Request.Cookie["UserName"];
            var account = string.Empty;
            if (false == token.DecryptToken(out account)) return false;
            var bsData = new BlogSysData();
            if (bsData.GetAccount().Equals(account)) return true;
            return false;
        }
    }
}